﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SEO.Entity;
using SEO.Business;

/// <summary>
/// 页面基础类
/// </summary>
#region 管理后台基础页面类
/// <summary>
/// 管理后台基础页面类
/// </summary>
public class AdminPage : System.Web.UI.Page
{
    /// <summary>
    /// 管理员账号
    /// </summary>
    protected string strAdminUserName = string.Empty;

    /// <summary>
    /// 管理员ID
    /// </summary>
    protected int nAdminUserID;

    /// <summary>
    /// 管理员姓名
    /// </summary>
    protected string strAdminName = string.Empty;

    /// <summary>
    /// 登陆日期
    /// </summary>
    protected string strAdminLoginTime = string.Empty;

    /// <summary>
    /// 管理员权限
    /// </summary>
    protected Int64 nAdminFeatures = 0;

    /// <summary>
    /// 重写预加载
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreLoad(EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        //验证管理员信息
        if (Session["AdminUserID"] != null && Session["AdminUserName"] != null)
        {
            strAdminUserName = Session["AdminUserName"].ToString();
            nAdminUserID = Convert.ToInt32(Session["AdminUserID"]);
            strAdminLoginTime = Convert.ToDateTime(Session["AdminLoginTime"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
            if (Session["AdminFeatures"] != null && Session["AdminFeatures"].ToString() != string.Empty)
                nAdminFeatures = Convert.ToInt64(Session["AdminFeatures"]);
            if (Session["AdminName"] != null)
                strAdminName = Session["AdminName"].ToString();
        }
        else
        {
            Response.Write("<script language='javascript'>top.location.href='Default.aspx';</script>");
            Response.End();
        }

        if (nAdminFeatures != 1)
        {
            if (!SupportFeatures(GetNeededFeatures()))
            {
                Response.Write("没有权限！");
                Response.End();
            }
        }
    }

    public virtual long GetNeededFeatures()
    {
        return 0;
    }

    /// <summary>
    /// 判断权限
    /// add by wilhelm
    /// </summary>
    /// <param name="nFeatures"></param>
    /// <returns></returns>
    public bool SupportFeatures(long nFeatures)
    {
        return ((nAdminFeatures & nFeatures) == nFeatures);
    }

    /// <summary>
    /// 获取所有关键字
    /// </summary>
    /// <returns></returns>
    protected DataRow[] GetKeywordsListBySortID(int nSortID)
    {
        WebCache webCache = new WebCache();
        DataTable dtKeywords = new DataTable();
        if (webCache.Has("AllKeywords"))
        {
            dtKeywords = (DataTable)webCache.Get("AllKeywords");
        }
        else
        {
            dtKeywords = new BisKeywords().GetAllListByName();
            webCache.Insert("AllKeywords", dtKeywords);
        }
        return dtKeywords.Select("SortID=" + nSortID.ToString());
    }

    /// <summary>
    /// 清除关键字缓存
    /// </summary>
    protected void RemoveKeywords()
    {
        new WebCache().Remove("AllKeywords");
    }

    /// <summary>
    /// 获取所有分类
    /// </summary>
    /// <returns></returns>
    protected DataTable GetAllSort()
    {
        WebCache webCache = new WebCache();
        DataTable dtSort = new DataTable();
        if (webCache.Has("AllSort"))
        {
            dtSort = (DataTable)webCache.Get("AllSort");
        }
        else
        {
            dtSort = new BisSort().GetList();
            webCache.Insert("AllSort", dtSort);
        }
        return dtSort;
    }

    /// <summary>
    /// 清除所有分类
    /// </summary>
    protected void RemoveAllSort()
    {
        new WebCache().Remove("AllSort");
    }

    /// <summary>
    /// 出错处理
    /// </summary>
    /// <param name="args"></param>
    protected override void OnError(EventArgs args)
    {
        Exception error = Context.Error;
        if (error != null)
        {
            string strUserName = "";
            if (Session["AdminUserName"] != null) strUserName = Session["AdminUserName"].ToString();
            string strErrorInfo = "\r\n错误描述："
                            + "\r\n用户：" + strUserName
                            + "\r\n来源:" + error.Source
                            + "\r\n方法：" + error.TargetSite.Name
                            + "\r\n堆栈：" + error.StackTrace
                            + "\r\n信息：" + error.Message
                            + "\r\nUrl：" + Request.Url.AbsoluteUri
                            + "\r\nIP：" + Common.GetVistorsIP();
            //记录日记
            Common.Log(strErrorInfo);
        }
        Response.Redirect("Error.aspx");
    }
}
#endregion
